<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\DevicesMap;
use App\Models\Device;
use App\Models\Manager;
use App\Models\PhoneNumber;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DeviceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '设备';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Device());

        $grid->tools(function ($tools) {
            $tools->append(new DevicesMap());
        });

        $grid->column('id', __('Id'));
        $grid->column('device_id', __('Device id'));
//        $grid->column('code', __('Code'));
        $grid->column('name', __('Name'));
//        $grid->column('manager.name', __('Manager name'));
//        $grid->column('manager_name', __('Manager name'));
//        $grid->column('manager_phone_number', __('Manager phone number'));
//        $grid->column('system_version', __('System version'));
//        $grid->column('brand', __('Brand'));
//        $grid->column('model', __('Model'));
//        $grid->column('rom', __('Rom'));
//        $grid->column('ram', __('Ram'));
        $grid->column('number_1', __('Number 1'));
        $grid->column('imei_1', __('Imei 1'));
        $grid->column('number_2', __('Number 2'));
        $grid->column('imei_2', __('Imei 2'));
        $grid->column('soc', __('Soc'));
//        $grid->column('updated_at', __('Updated at'));
        $grid->column('heart_at', __('Heart at'))->sortable();
        $grid->column('location_at', __('Location at'))->sortable();
        $grid->column('last_address', __('最后定位地址'))->display(function () {
            return "<a target='_blank' href='http://api.map.baidu.com/marker?location=$this->latitude,$this->longitude&title=$this->last_address&content=$this->last_address&output=html'>$this->last_address</a>";

        });
        $grid->column('sms_at', __('Sms at'))->sortable();
        $grid->column('count_sms', __('短信总计'))->display(function () {
            return "<a href='" . url('admin/sms-logs') . "?" . http_build_query(['device_id' => $this->device_id]) . "'>$this->count_sms</a>";
        });;
        $grid->column('visit_web_at', __('Visit web at'))->sortable();
        $grid->column('count_visit_web', __('网页访问总计'))->display(function () {
            return "<a href='" . url('admin/visit-web-tasks') . "?" . http_build_query(['device_id' => $this->device_id]) . "'>$this->count_visit_web</a>";
        });

//        $grid->column('created_at', __('Created at'));
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->equal('manager_id', __('Manager id'))->select(Manager::all()->pluck('name', 'id'));
            $filter->like('name', __('Name'));
            $filter->equal('number_1', __('Number 1'));
            $filter->equal('number_2', __('Number 2'));
            $filter->between('heart_at', __('Heart at'))->datetime();
            $filter->between('location_at', __('Location at'))->datetime();
            $filter->like('last_address', __('Last address'));
            $filter->between('sms_at', __('Sms at'))->datetime();
            $filter->between('visit_web_at', __('Visit web at'))->datetime();

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Device::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('device_id', __('Device id'));
        $show->field('code', __('Code'));
        $show->field('name', __('Name'));
        $show->field('manager.name', __('Manager name'));
        $show->field('manager_phone_number', __('Manager phone number'));
        $show->field('system_version', __('System version'));
        $show->field('brand', __('Brand'));
        $show->field('model', __('Model'));
        $show->field('rom', __('Rom'));
        $show->field('ram', __('Ram'));
        $show->field('number_1', __('Number 1'));
        $show->field('imei_1', __('Imei 1'));
        $show->field('number_2', __('Number 2'));
        $show->field('imei_2', __('Imei 2'));
        $show->field('updated_at', __('Updated at'));
        $show->field('created_at', __('Created at'));
        $show->field('location_at', __('Location at'));
        $show->field('heart_at', __('Heart at'));
        $show->field('sms_at', __('Sms at'));
        $show->field('visit_at', __('Visit at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Device());

        $form->text('name', __('Name'))->rules();
        $form->text('device_id', __('Device id'))->rules('required');
        $models = Manager::all();
        $list = [];
        foreach ($models as $model) {
            $list[$model->id] = "$model->name $model->phone";

        }
        $form->select('manager_id', __('Manager id'))->options($list);
//        $form->text('manager_name', __('Manager name'))->rules('required');;
//        $form->text('manager_phone_number', __('Manager phone number'))->rules('required');
        $form->number('system_version', __('System version'));
        $form->text('brand', __('Brand'));
        $form->text('model', __('Model'));
        $form->number('rom', __('Rom'));
        $form->number('ram', __('Ram'));
        $form->select('phone_number_1_id', __('Phone number 1 id'))->options(PhoneNumber::all()->pluck('imei', 'id'));
        $form->select('phone_number_2_id', __('Phone number 2 id'))->options(PhoneNumber::all()->pluck('imei', 'id'));

//        $form->mobile('number_1', __('Number 1'))->rules('regex:/^1[345789][0-9]{9}$/|unique:devices');;
//        $form->text('imei_1', __('Imei 1'))->rules('required');;
//        $form->mobile('number_2', __('Number 2'))->rules('regex:/^1[345789][0-9]{9}$/|unique:devices');;
//        $form->text('imei_2', __('Imei 2'))->rules('');;
//        $form->datetime('location_at', __('Location at'))->default(date('Y-m-d H:i:s'));
        return $form;
    }
}
